package com.samoilovich.belarustoday.afisha.films;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.samoilovich.belarustoday.basefragments.BaseFragment;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.datemodel.CinemaModel;
import com.samoilovich.belarustoday.tools.Utility;

public class CinemaFragment extends BaseFragment{
    private final float ZOOM = 15.5f;
    private static CinemaModel mCurrentCinema;
    private SupportMapFragment mMapFragment;
    private GoogleMap mMapView;

    public CinemaFragment() {
    }

    public CinemaFragment(CinemaModel cinema) {
        mCurrentCinema = cinema;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_map, null);
        prepareActionBarViews(R.string.empty_str, R.string.seances, true);
        prepareMap();

        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        setCinemaLocation();
    }

    @Override
    public void updateAllData(Context context) {
        if (mMapFragment != null && mMapView != null) {
            setCinemaLocation();
        }
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    private void prepareMap() {
        mMapFragment = SupportMapFragment.newInstance();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();

        transaction.replace(R.id.frameLayout_map, mMapFragment);
        transaction.commit();
    }

    private void setCinemaLocation() {
        LatLng position = new LatLng(Double.valueOf(mCurrentCinema.getLatitude()),Double.valueOf(mCurrentCinema.getLongitude()));

        mMapView = mMapFragment.getMap();
        if (mMapView != null) {
            Marker marker = mMapView.addMarker(new MarkerOptions().position(position).
                    icon(BitmapDescriptorFactory.fromResource(R.drawable.cinema_marker)).
                    title(mCurrentCinema.getName()).
                    snippet(mCurrentCinema.getPlace()));
            marker.showInfoWindow();
            mMapView.animateCamera(CameraUpdateFactory.newLatLngZoom(position, ZOOM));
        }
    }

}
